from fastapi import APIRouter,Depends
from pydantic import BaseModel

from backend.app.app.crud_pro.tb_userpro_out import get_name_year, get_all_data, out_del, out_data_add, \
    get_eval_result
from backend.app.app.models.project_data import save_out_data
from backend.app.app.api.api_v1.user import verify_token_user

curve_router = APIRouter(prefix="/curve_data", tags=["变化曲线"])

@curve_router.post("/get_data", name="读取名称,措施,年份")
async def get_name_(ver=Depends(verify_token_user)):
    name_year = get_name_year()
    return {
        "result": name_year,
        "verify": ver
    }

class read_(BaseModel):
    name : str
    type : str
    up_year: str
    low_year: str


@curve_router.post("/get_lib",name="读库")
async def get_lib(querydata: read_, ver=Depends(verify_token_user)):
    data = get_all_data(dict(querydata))
    return {
        "data": data,
        "verify": ver
    }

@curve_router.post("/read_calculation_result",name="读取评价计算结果")
async def read_calculation_result(ver = Depends(verify_token_user)):
    re = get_eval_result()
    return {"result":re,"verify":ver}

@curve_router.post("/save_lib",name="存库")
async def save_lib(save_data: save_out_data,ver = Depends(verify_token_user)):
    if save_data.add:
        add = out_data_add(save_data.add)
    else:
        add = {}
    if save_data.delete:
        delete = out_del(save_data.delete)
    else:
        delete = {}
    return {
        "add": add,
        "delete": delete,
        "verify": ver
    }



